#include "Exp0.h"

void Exp0()
  {
    cleardevice();
    int Still = 1;
    Object Cursor, *pCursor = &Cursor;
    Bulb BulbArr[4], *pBulb = BulbArr;

    InitExp0Cursor(pCursor);
    InitBulb(pBulb);

    char Map[350], *pMap = Map;
    ReadMapN(pMap, 0);

    OptIntro(0);

    while (Still)
      {
        OptMap0();
        OptCursor(pCursor);
        PlayerAct(pCursor, pMap, &Still);
        LightBulb(pCursor, BulbArr);
        if (CountLightedBublb(BulbArr) < 4)
          SparkLetter(BulbArr);
        OptLightBulb(BulbArr);
        FlushBatchDraw();
        if (CountLightedBublb(BulbArr) == 4)
          Exp0Solved(&Still);
      }
    OptMenuBk();
  }
void InitBulb(Bulb *pBulb)
  {
    for ( int i = 0; i < 4; i++, pBulb++)
      pBulb->Open = 0;
  }
void InitExp0Cursor(Object *pCursor)
  {
    pCursor->HP = 10;
    pCursor->X = 665, pCursor->Y = 310;
    pCursor->GridX = 13, pCursor->GridY = 6;
  }
void OptMap0()
  {
    IMAGE Map0;
    loadimage(&Map0, _T("Pic/Exp0/Map0.png"));
    putimage(0, 0, &Map0);
  }

int CountLightedBublb(Bulb *pBulb)
  {
    int count = 0;
    for ( int i = 0; i < 4; i++, pBulb++)
        if (pBulb->Open == 1)
          count++;
    return count;
  }
void SparkLetter(Bulb BulbArr[])
  {
    if (CountLightedBublb(BulbArr) == 0)
      if (BulbArr[2].Open == 0)//Down
        SparkDown(675, 560);

    if (CountLightedBublb(BulbArr) == 1)
      if (BulbArr[0].Open == 0)//Up
        SparkUp(675, 60);

    if (CountLightedBublb(BulbArr) == 2)
    if (BulbArr[1].Open == 0)//Right
      SparkRight(965, 310);

    if (CountLightedBublb(BulbArr) == 3)
      if (BulbArr[3].Open == 0)//Left
        SparkLeft(365, 310);
  }
void SparkUp(int X, int Y)
  {
    IMAGE ImgK;
    loadimage(&ImgK, _T("Pic/Exp0/k.png"));
    TransparentImage(NULL, X, Y, &ImgK);
  }
void SparkRight(int X, int Y)
  {
    IMAGE ImgL;
    loadimage(&ImgL, _T("Pic/Exp0/l.png"));
    TransparentImage(NULL, X, Y, &ImgL);
  }
void SparkDown(int X, int Y)
  {
    IMAGE ImgJ;
    loadimage(&ImgJ, _T("Pic/Exp0/j.png"));
    TransparentImage(NULL, X, Y, &ImgJ);
  }
void SparkLeft(int X, int Y)
  {
    IMAGE ImgH;
    loadimage(&ImgH, _T("Pic/Exp0/h.png"));
    TransparentImage(NULL, X, Y, &ImgH);
  }

void LightBulb(Object *pCursor, Bulb BulbArr[])
  {
    int GridX = pCursor->GridX, GridY = pCursor->GridY;
    if (GridX == 13)
      if (GridY == 1)
          BulbArr[0].Open = 1;
    if (GridX == 19)
      if (GridY == 6)
          BulbArr[1].Open = 1;
    if (GridX == 13)
      if (GridY == 11)
          BulbArr[2].Open = 1;
    if (GridX == 7)
      if (GridY == 6)
          BulbArr[3].Open = 1;
  }
void OptLightBulb(Bulb BulbArr[])
  {
    if (BulbArr[0].Open == 1)
      OptUpLight(641, 34);
    if (BulbArr[1].Open == 1)
      OptRightLight(881, 286);
    if (BulbArr[2].Open == 1)
      OptDownLight(641, 491);
    if (BulbArr[3].Open == 1)
      OptLeftLight(333, 286);
  }
void OptLeftLight(int X, int Y)
  {
    IMAGE ImgLeftLight;
    loadimage(&ImgLeftLight, _T("Pic/Exp0/LeftLight.png"));
    TransparentImage(NULL, X, Y, &ImgLeftLight);
  }
void OptRightLight(int X, int Y)
  {
    IMAGE ImgRightLight;
    loadimage(&ImgRightLight, _T("Pic/Exp0/RightLight.png"));
    TransparentImage(NULL, X, Y, &ImgRightLight);
  }
void OptUpLight(int X, int Y)
  {
    IMAGE ImgUpLight;
    loadimage(&ImgUpLight, _T("Pic/Exp0/UpLight.png"));
    TransparentImage(NULL, X, Y, &ImgUpLight);
  }
void OptDownLight(int X, int Y)
  {
    IMAGE ImgDownLight;
    loadimage(&ImgDownLight, _T("Pic/Exp0/DownLight.png"));
    TransparentImage(NULL, X, Y, &ImgDownLight);
  }
void Exp0Solved(int *Still)
  {
    Accomplish('0');
    *Still = 0;
  }
